﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web;
using Microsoft.SharePoint;
using Slam.FieldTypes;

namespace SlamCms.SharePoint.FieldTypes
{
	public partial class EnhancedFileUploadFieldEditor : UserControl, IFieldEditor
	{	
		#region IFieldEditor Members

		public bool DisplayAsNewSection
		{
			get { return true; }
		}

		protected override void CreateChildControls()
		{
			base.CreateChildControls();

			ddlStorageMode.AutoPostBack = true;
			ddlStorageMode.SelectedIndexChanged += new EventHandler(ddlStorageMode_SelectedIndexChanged);
		}

		public void InitializeWithField(Microsoft.SharePoint.SPField field)
		{
            SlamField uploadField = field as SlamField;

			if (!Page.IsPostBack)
			{
				SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);

				foreach (SPWeb site in siteCollection.AllWebs)
				{
					foreach (SPList list in site.Lists)
					{
						if (!list.Hidden && (list.BaseTemplate == SPListTemplateType.DocumentLibrary||list.BaseTemplate == SPListTemplateType.PictureLibrary))
						{
                            if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
							    ddlSaveList.Items.Add(new ListItem((String.IsNullOrEmpty(site.Name) ? "[Root]" : site.Name) + " - " + list.Title, list.ID.ToString()));
                            if (list.BaseTemplate == SPListTemplateType.PictureLibrary)
                                ddSavePictureList.Items.Add(new ListItem((String.IsNullOrEmpty(site.Name) ? "[Root]" : site.Name) + " - " + list.Title, list.ID.ToString()));
						}
					}
				}

				if (uploadField != null)
				{
					string storageMode = (string)uploadField.GetCustomPropertyValue("StorageMode");
					string selectedDocumentLibrary = (string)uploadField.GetCustomPropertyValue("SaveDocumentLibrary");
                    string selectedPictureLibrary = (string)uploadField.GetCustomPropertyValue("SavePictureLibrary");
					txtSaveLocation.Text = (string)uploadField.GetCustomPropertyValue("SaveLocation");
					txtAllowedFileExtensions.Text = (string)uploadField.GetCustomPropertyValue("AllowedFileExtensions");
					txtBucket.Text = (string)uploadField.GetCustomPropertyValue("S3Bucket");
					txtAccessKey.Text = (string)uploadField.GetCustomPropertyValue("S3AccessKey");
					txtSecretKey.Text = (string)uploadField.GetCustomPropertyValue("S3SecretKey");
					if(uploadField.GetCustomPropertyValue("UniqueFileNames") != null)
						chkUniqueFileNames.Checked = (bool)uploadField.GetCustomPropertyValue("UniqueFileNames");

					if (uploadField.GetCustomPropertyValue("CreateUniqueFolders") != null)
						chkCreateFolders.Checked = (bool)uploadField.GetCustomPropertyValue("CreateUniqueFolders");

                    if (uploadField.GetCustomPropertyValue("MaxFilesPerListItem") != null)
                        txtMaxFilesPerListItem.Text = ((int)uploadField.GetCustomPropertyValue("MaxFilesPerListItem")).ToString();

					ListItem storageModeItem = ddlStorageMode.Items.FindByValue(storageMode);
					if (storageModeItem != null)
					{
						storageModeItem.Selected = true;
					}
					else
					{
						ddlStorageMode.SelectedIndex = 0;
					}

					ListItem selecteDocumentLibItem = ddlSaveList.Items.FindByValue(selectedDocumentLibrary);
                    if (selecteDocumentLibItem != null)
                        selecteDocumentLibItem.Selected = true;

                    ListItem selectedPictureLibItem = ddSavePictureList.Items.FindByValue(selectedPictureLibrary);
                    if (selectedPictureLibItem != null)
                        selectedPictureLibItem.Selected = true;
				}

				SetSectionVisibility();
			}
		}

		private void SetSectionVisibility()
		{
			ifcAmazonSignature.Visible = false;
			ifcAmazonBucket.Visible = false;
			ifcAmazonAccessKey.Visible = false;
			ifcDocumentLibrary.Visible = false;
            ifcPictureLibrary.Visible = false;
			ifcFileSystem.Visible = false;

			string storageMode = ddlStorageMode.SelectedValue;
			switch (storageMode)
			{
				case "Amazon S3":
					ifcAmazonSignature.Visible = true;
					ifcAmazonBucket.Visible = true;
					ifcAmazonAccessKey.Visible = true;
					break;
				case "Document Library":
					ifcDocumentLibrary.Visible = true;
					break;
                case "Picture Library":
                    ifcPictureLibrary.Visible = true;
                    break;
				default:
					ifcFileSystem.Visible = true;
					break;
			}
		}

		protected void ddlStorageMode_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetSectionVisibility();
		}

		public void OnSaveChange(Microsoft.SharePoint.SPField field, bool isNewField)
		{
            SlamField uploadField = field as SlamField;

			uploadField.SetCustomProperty("StorageMode", typeof(string), ddlStorageMode.SelectedValue);
			uploadField.SetCustomProperty("SaveDocumentLibrary", typeof(string), ddlSaveList.SelectedValue);
            uploadField.SetCustomProperty("SavePictureLibrary", typeof(string), ddSavePictureList.SelectedValue);
			uploadField.SetCustomProperty("SaveLocation", typeof(string), txtSaveLocation.Text);
			uploadField.SetCustomProperty("AllowedFileExtensions", typeof(string), txtAllowedFileExtensions.Text);
			uploadField.SetCustomProperty("S3Bucket", typeof(string), txtBucket.Text);
			uploadField.SetCustomProperty("S3AccessKey", typeof(string), txtAccessKey.Text);
			uploadField.SetCustomProperty("S3SecretKey", typeof(string), txtSecretKey.Text);
			uploadField.SetCustomProperty("UniqueFileNames", typeof(bool), chkUniqueFileNames.Checked);
			uploadField.SetCustomProperty("CreateUniqueFolders", typeof(bool), chkCreateFolders.Checked);
            uploadField.SetCustomProperty("MaxFilesPerListItem", typeof(int), int.Parse(txtMaxFilesPerListItem.Text));
		}

		#endregion

	}
}
